import 'package:dio/dio.dart';

class WeatherInfo {
  String? city;
  String adcode;
  String province;
  String reporttime;
  List<CastInfo> casts;

  WeatherInfo(
    this.city,
    this.adcode,
    this.province,
    this.reporttime,
    this.casts,
  );

  WeatherInfo.fromJson(Map<String, dynamic> json)
    : city = json['city'],
      adcode = json['adcode'],
      province = json['province'],
      reporttime = json['reporttime'],
      casts = (json['casts'] as List).map((e) => CastInfo.fromJson(e)).toList();

  @override
  String toString() {
    return 'WeatherInfo{ city: $city, adcode: $adcode, province: $province, reporttime: $reporttime, casts: $casts }';
  }
}

class CastInfo {
  String date;
  String week;
  String dayweather;
  String nightweather;
  String daytemp;
  String nighttemp;
  String daywind;
  String nightwind;
  String daypower;
  String nightpower;
  String daytemp_float;
  String nighttemp_float;

  CastInfo(
    this.date,
    this.week,
    this.dayweather,
    this.nightweather,
    this.daytemp,
    this.nighttemp,
    this.daywind,
    this.nightwind,
    this.daypower,
    this.nightpower,
    this.daytemp_float,
    this.nighttemp_float,
  );

  CastInfo.fromJson(Map<String, dynamic> json)
    : date = json['date'],
      week = json['week'],
      dayweather = json['dayweather'],
      nightweather = json['nightweather'],
      daytemp = json['daytemp'],
      nighttemp = json['nighttemp'],
      daywind = json['daywind'],
      nightwind = json['nightwind'],
      daypower = json['daypower'],
      nightpower = json['nightpower'],
      daytemp_float = json['daytemp_float'],
      nighttemp_float = json['nighttemp_float'];

  @override
  String toString() {
    return 'CastInfo{ date: $date, week: $week, dayweather: $dayweather, nightweather: $nightweather, daytemp: $daytemp, nighttemp: $nighttemp, daywind: $daywind, nightwind: $nightwind, daypower: $daypower, nightpower: $nightpower, daytemp_float: $daytemp_float, nighttemp_float: $nighttemp_float }';
  }
}

class WeatherService {
  static Future<void> getWeather(String cityCode) async {
    Dio dio = Dio();
    var res = await dio.get(
      'https://restapi.amap.com/v3/weather/weatherInfo?city=$cityCode&key=9db1b21c9bc15e7b2f74a7fe616c2914&extensions=all',
    );
    var weatherInfo = WeatherInfo.fromJson(res.data['forecasts'][0]);
    print(weatherInfo);
  }
}
